package com.ztesoft.framework.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author wsmp@zte.com.cn
 * 
 */
public final class Utils {

	private Utils() {
	}

	/**
	 * 判断对象数组是否为空
	 * 
	 * @param objs
	 *            Object[]
	 * @return boolean
	 */
	public static boolean notEmpty(Object[] objs) {
		return !isEmpty(objs);
	};

	public static boolean isRunningOnWindows() {
		String OS = System.getenv("OS");
		return (OS != null && OS.toLowerCase().contains("win"));
	}

	public static boolean isEmpty(Object[] objs) {
		if (objs == null || objs.length == 0) {
			return true;
		}
		return false;
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 *            String
	 * @return boolean
	 */
	public static boolean notEmpty(String str) {
		return !isEmpty(str);
	}

	public static boolean isEmpty(String str) {
		if (str == null || str.length() == 0) {
			return true;
		}

		return false;
	}

	/**
	 * 判断集合]是否为空
	 * 
	 * @param coll
	 *            Collection
	 * @return boolean
	 */
	public static boolean notEmpty(Collection<?> coll) {
		return !isEmpty(coll);
	}

	public static boolean isEmpty(Collection<?> coll) {
		if (coll == null || coll.size() == 0) {
			return true;
		}

		return false;
	}

	/**
	 * 判断int数组是否为空
	 * 
	 * @param intArr
	 *            int[]
	 * @return boolean
	 */
	public static boolean notEmpty(int[] arg) {
		return !isEmpty(arg);
	}

	public static boolean isEmpty(int[] intArr) {
		if (intArr == null || intArr.length == 0) {
			return true;
		}

		return false;
	}

	/**
	 * 判断long数组是否为空
	 * 
	 * @param longArr
	 *            long[]
	 * @return boolean
	 */
	public static boolean notEmpty(long[] arg) {
		return !isEmpty(arg);
	}

	public static boolean isEmpty(long[] longArr) {
		if (longArr == null || longArr.length == 0) {
			return true;
		}

		return false;
	}

	/**
	 * 判断Map是否为空
	 * 
	 * @param map
	 *            Map
	 * @return boolean
	 */
	public static boolean notEmpty(Map<?, ?> arg) {
		return !isEmpty(arg);
	}

	public static boolean isEmpty(Map<?, ?> map) {
		if (map == null || map.size() == 0) {
			return true;
		}

		return false;
	}

	public static boolean notEmpty(Object arg) {
		return !isEmpty(arg);
	}

	public static boolean isEmpty(Object obj) {
		if (obj == null) {
			return true;
		}
		return false;
	}

	/**
	 * 判断int值是否空值
	 * 
	 * @param val
	 *            int
	 * @return boolean
	 */
	public static boolean notEmpty(int arg) {
		return !isEmpty(arg);
	}

	public static boolean isEmpty(int val) {
		return (val == FrameWorkConstants.NULL_INT);
	}

	/**
	 * 判断long值是否空值
	 * 
	 * @param val
	 *            long
	 * @return boolean
	 */
	public static boolean notEmpty(long arg) {
		return !isEmpty(arg);
	}

	public static boolean isEmpty(long val) {
		return (val == FrameWorkConstants.NULL_LONG);
	}

	/**
	 * 判断float值是否空值
	 * 
	 * @param val
	 *            float
	 * @return boolean
	 */
	public static boolean notEmpty(float arg) {
		return !isEmpty(arg);
	}

	public static boolean isEmpty(float val) {
		return (val == FrameWorkConstants.NULL_FLOAT);
	}

	/**
	 * 判断double值是否空值
	 * 
	 * @param val
	 *            double
	 * @return boolean
	 */
	public static boolean notEmpty(double arg) {
		return !isEmpty(arg);
	}

	public static boolean isEmpty(double val) {
		return (val == FrameWorkConstants.NULL_DOUBLE);
	}

	/**
	 * 判断一个字符串是否数字
	 * 
	 * @param val
	 *            String
	 * @return boolean
	 */
	public static boolean notNumber(String arg) {
		return !isNumber(arg);
	}

	public static boolean isNumber(String val) {
		try {
			Double.parseDouble(val);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 除数
	 */
	public static long DIV = 10000;

	/**
	 * 得到float数据
	 * 
	 * @param value
	 *            long
	 * @return float
	 */
	public static float divLongToFloat(long value) {
		float ret = ((float) value) / DIV;
		return ret;
	}

	public static double divLongToDouble(long value) {
		double ret = ((double) value) / DIV;
		return ret;
	}

	/**
	 * 换成字符串插入数据库
	 * 
	 * @param value
	 *            long
	 * @return String
	 */
	public static String divLongToString(long value) {
		String ret = Double.toString(((double) value) / DIV);
		return ret;
	}

	/**
	 * 小数点处理
	 * 
	 * @param ll
	 *            long
	 * @param deciLen
	 *            int
	 * @return String
	 */
	public static String fomartLong(long ll, int deciLen) {
		// 小数点后位数
		String strMoney = Long.toString(ll);
		StringBuffer retSb = new StringBuffer();
		// 如果为负数

		if (strMoney.substring(0, 1).equals("-")) {
			// 第一个为负号
			retSb.append("-");
			// 实际数字长度，去掉负号的
			strMoney = strMoney.substring(1);
		}
		// 字符串长度

		int len = strMoney.length();
		// 验证长度，不满或者4位数前补零

		if (len <= deciLen) {
			// 补零的位数

			int iRex = deciLen - len;
			retSb.append("0").append(".");
			for (int i = 0; i < iRex; i++) {
				retSb.append("0");
			}
			retSb.append(strMoney.substring(0));
		} else {
			// 超过4位的，移小数点

			int offset = len - deciLen;
			retSb.append(strMoney.substring(0, offset)).append(".").append(strMoney.substring(offset));
		}
		// 去掉最后几位

		String ret = retSb.toString();
		for (int i = 0; i < 4; i++) {
			if (!ret.substring(ret.length() - 1).equals("0")) {
				break;
			}
			ret = ret.substring(0, ret.length() - 1);
			if (i == 3) {
				ret = ret.substring(0, ret.length() - 1);
			}
		}
		return ret;
	}

	/**
	 * 得到long数据
	 * 
	 * @param value
	 *            long
	 * @return float
	 */
	public static long mulFloatToLong(float value) {
		return (long) (value * 10000);
	}

	/**
	 * 去掉字符串的空格
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public static String trim(String str) {
		return (str == null) ? str : str.trim();
	}

	/**
	 * 字符串是否在字符传集合中被找到
	 * 
	 * @param str
	 *            String
	 * @param collArr
	 *            String[]
	 * @return boolean true-找到, false-未找到
	 */
	public static boolean exists(String str, String[] collArr) {
		if (isEmpty(str) || isEmpty(collArr)) {
			return false;
		}

		for (int i = 0; i < collArr.length; i++) {
			if (str.equals(collArr[i])) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 某数字是否在数组里面
	 * 
	 * @param j
	 *            int
	 * @param collArr
	 *            int[]
	 * @return boolean true-找到, false-未找到
	 */
	public static boolean exists(int j, int[] collArr) {
		if (Utils.isEmpty(collArr)) {
			return false;
		}
		for (int i = 0; i < collArr.length; i++) {
			if (j == collArr[i]) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 字符串是否在列表中被找到
	 * 
	 * @param str
	 *            String 要查找的字符串
	 * 
	 * @param list
	 *            List 字符串列表, 将调用list.get(index).toString()方法和str比较
	 * @return boolean
	 */
	public static boolean exists(String str, List<?> list) {
		if (isEmpty(str) || isEmpty(list)) {
			return false;
		}

		for (int i = 0; i < list.size(); i++) {
			if (str.equals(list.get(i).toString())) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 
	 * <把id标识数组转换为以','分割的字符串>
	 * 
	 * @param idArr
	 * @return
	 */
	public static String convertIdArr2StringByComma(int[] idArr) {
		StringBuffer retStr = new StringBuffer();
		if (isEmpty(idArr)) {
			return retStr.toString();
		}

		for (int i = 0; i < idArr.length; i++) {
			retStr.append(String.valueOf(idArr[i])).append(",");
		}

		return retStr.substring(0, retStr.length() - 1);
	}

	/**
	 * 
	 * <把id标识数组转换为以','分割的字符串>
	 * 
	 * @param idArr
	 * @return
	 */
	public static String convertIdArr2StringByComma(long[] idArr) {
		StringBuffer retStr = new StringBuffer();
		if (isEmpty(idArr)) {
			return retStr.toString();
		}

		for (int i = 0; i < idArr.length; i++) {
			retStr.append(String.valueOf(idArr[i])).append(",");
		}

		return retStr.substring(0, retStr.length() - 1);
	}

	/**
	 * 
	 * <把id标识数组转换为以','分割的字符串>
	 * 
	 * @param idArr
	 * @return
	 */
	public static String convertIdArr2StringByComma(String[] idArr) {
		StringBuffer retStr = new StringBuffer();
		if (isEmpty(idArr)) {
			return retStr.toString();
		}

		for (int i = 0; i < idArr.length; i++) {
			retStr.append(String.valueOf(idArr[i])).append(",");
		}

		return retStr.substring(0, retStr.length() - 1);
	}

	/**
	 * 字符串数组转换为LONG数组</br> 不对入参进行非空判断
	 * 
	 * @param strArr
	 *            字符串数组
	 * 
	 * @return Long[]
	 * 
	 * @author xu.yang22@zte.com.cn 2011-8-25
	 * @see [类、类#方法、类#成员]
	 */
	public static Long[] stringArr2longArr(String[] strArr) {
		Long[] longArr = new Long[strArr.length];

		for (int i = 0, n = strArr.length; i < n; i++) {
			longArr[i] = Long.parseLong(strArr[i]);
		}

		return longArr;
	}

	/**
	 * @param map
	 * @param key
	 * @return
	 */
	public static String getString(Map<?, ?> map, String key) {
		String retStr = "";
		Object object = map.get(key);
		if (null != object) {
			retStr = object.toString();
		}
		return retStr;
	}

	/**
	 * @param map
	 * @param key
	 * @return
	 */
	public static Long getLong(Map<?, ?> map, String key) {
		Long ret = 0l;
		Object object = map.get(key);
		if (null != object) {
			ret = Long.parseLong(object.toString());
		}
		return ret;
	}

	/**
	 * list集合转str add by lin.weibin
	 * 
	 * @param list
	 *            String集合
	 * @return
	 */
	public static String convertIdList2StringByComma(List<String> strList) {
		//
		StringBuffer retStr = new StringBuffer();
		if (isEmpty(strList)) {
			return retStr.toString();
		}
		for (int i = 0; i < strList.size(); i++) {
			if (strList.get(i)==null) continue;
			retStr.append(strList.get(i)).append(",");
		}
		return retStr.substring(0, retStr.length() - 1);
	}
	
	public static String convertIdList2StringBy(List<String> strList, String split, int count) {
		//
		StringBuffer retStr = new StringBuffer();
		if (isEmpty(strList)) {
			return retStr.toString();
		}
		for (int i = 0; i < strList.size(); i++) {
			retStr.append(strList.get(i)).append(split);
		}
		return retStr.substring(0, retStr.length() - count);
	}

	/**
	 * list去重 add by lin.weibin
	 * 
	 * @param list
	 *            str集合
	 * @return
	 */
	public static List<String> removeDuplicate(List<String> list) {
		Set<String> hashSet = new HashSet<String>(list);
		list.clear();
		list.addAll(hashSet);
		return list;
	}

	/**
	 * 数组组合 add by lin.weibin
	 * 
	 * @param a0
	 *            String[]
	 * @param a1
	 *            String[]
	 * @return a0 * a1
	 */
	public static String[] buildArr(String[] a0, String[] a1) {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < a0.length; i++) {
			for (int j = 0; j < a1.length; j++) {
				list.add(new StringBuilder().append(a0[i]).append(",").append(a1[j]).toString());
			}
		}
		a0 = new String[list.size()];
		list.toArray(a0);
		return a0;
	}

	/**
	 * 占位符替换 add by lin.weibin
	 * 
	 * @param str
	 *            String
	 * @param arr
	 *            String[]
	 * @return
	 */
	public static String fillStringByArgs(String str, String[] arr) {
		Matcher m = Pattern.compile("\\{(\\d)\\}").matcher(str);
		while (m.find()) {
			str = str.replace(m.group(), arr[Integer.parseInt(m.group(1))]);
		}
		return str;
	}

	/**
	 * str 转为list
	 * 
	 * @param String
	 *            list 集合
	 * @return
	 */
	public static List<String> convertIdString2ListByComma(String str) {
		//

		List<String> strList = new ArrayList<String>();
		String[] d = str.split(",");
		for (int i = 0; i < d.length; i++) {
			strList.add(d[i]);
		}
		return strList;
	}

	/**
	 * str (sign 分隔符) 转为list
	 * 
	 * @param String
	 *            list 集合
	 * @return
	 */
	public static List<String> convertIdString2ListByComma(String str, String split) {
		//

		List<String> strList = new ArrayList<String>();
		String[] d = str.split(split);
		for (int i = 0; i < d.length; i++) {
			strList.add(d[i]);
		}
		return strList;
	}

	public static List<Integer> convertIdInteger2ListByComma(String str) {
		//

		List<Integer> intList = new ArrayList<Integer>();
		String[] d = str.split(",");
		for (int i = 0; i < d.length; i++) {
			intList.add(Integer.valueOf(d[i]));
		}
		return intList;
	}
	public static int countArrValue(int key, int[] obj) {
		//
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (int i = 0, j = obj.length; i < j; i++) {
			if (map.get(obj[i]) == null) {
				map.put(obj[i], 1);
			} else {
				map.put(obj[i], map.get(obj[i]) + 1);
			}
		}
		return map.get(key);
	}

	public static String convertIdList2Integer(List<Integer> strList) {
		//
		StringBuffer retStr = new StringBuffer();
		if (isEmpty(strList)) {
			return retStr.toString();
		}
		for (int i = 0; i < strList.size(); i++) {
			retStr.append(strList.get(i)).append(",");
		}
		return retStr.substring(0, retStr.length() - 1);
	}

	/**
     * 读取输出流数据
     * 
     * @param p 进程
     * @return 从输出流中读取的数据
     * @throws IOException
     */
    public static final String getShellOut(Process p) throws IOException{
        
        StringBuilder sb = new StringBuilder();
        BufferedInputStream in = null;
        BufferedReader br = null;
        
        try {
            
            in = new BufferedInputStream(p.getInputStream());
            br = new BufferedReader(new InputStreamReader(in));
            String s;
            while ((s = br.readLine()) != null) {
                sb.append("\r\n").append(s);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            br.close();
            in.close();
        }
        
        return sb.toString();
    }
    

	/**
	 * 要求：三个数约定长度必须一致，这里不做代码层面验证..
	 * @param date
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
    public static boolean isInRange(String date, String beginDate, String endDate) {

        boolean asc = isAsc(beginDate, endDate);
        if (asc) {
            if (date.compareTo(beginDate) >= 0 && date.compareTo(endDate) <= 0) {
                return true;
            }
        } else {
            if (date.compareTo(beginDate) >= 0 || date.compareTo(endDate) <= 0) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 判断字符串是否升序的, 第一个字符串小于第二个字符串
     * @param firstStr String
     * @param secondStr String
     * @return boolean true-升序, false-降序
     */
    public static boolean isAsc(String firstStr, String secondStr) {
        return (firstStr.compareTo(secondStr) < 0);
    }

}
